package com.lanjiu.lib.utils.json;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lanjiu.lib.utils.CollectionUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/7 22:35
 */
public class JsonUtils {
    private static Gson gson = new Gson();

    private JsonUtils() {
    }

    public static <T> String toJson(Object cls) {
        if (cls == null) return null;
        return gson.toJson(cls);
    }

    public static <T> T fromJson(String result, Class<T> cls) {
        if (TextUtils.isEmpty(result)) return null;
        return gson.fromJson(result, cls);
    }

    public static <T> T fromJson(String result, Type type) {
        if (TextUtils.isEmpty(result)) return null;
        return gson.fromJson(result, type);
    }

    public static <K, V> String convertMapToJson(Map<K, V> map) {
        if (CollectionUtils.isEmpty(map)) return null;
        return gson.toJson(map);
    }

    public static <K, V> Map<K, V> convertJsonToMap(String json, Class<K> keyClass, Class<V> valClass) {
        try {
            if (TextUtils.isEmpty(json)) return null;
            Type type = new TypeToken<Map<K, V>>() {
            }.getType();
            return gson.fromJson(json, type);
        } catch (JsonSyntaxException e) {
            return null;
        }
    }

    public static <E> String convertListToJson(List<E> list) {
        if (CollectionUtils.isEmpty(list)) return "";
        StringBuilder builder = new StringBuilder();
        for (E e : list) {
            builder.append(",");
            builder.append(gson.toJson(e));
        }
        if (builder.length() > 1)
            builder.deleteCharAt(0);
        return "[" + builder.toString() + "]";
    }

    public static <E> List<E> convertJsonToList(String json, Class<E> clasz) {
        if (TextUtils.isEmpty(json)) return null;
        List<E> data = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(json);
            for (int i = 0; i < array.length(); i++) {
                data.add(gson.fromJson(array.optString(i), clasz));
            }
            return data;
        } catch (JSONException e) {
            return null;
        }
    }

    public static String convertStringMapToJson(Map<String, String> map) {
        if (CollectionUtils.isEmpty(map)) return null;
        return gson.toJson(map);
    }

    public static Map<String, String> convertJsonToMapString(String json) {
        try {
            if (TextUtils.isEmpty(json)) return null;
            Type type = new TypeToken<Map<String, String>>() {
            }.getType();
            return gson.fromJson(json, type);
        } catch (JsonSyntaxException e) {
            return null;
        }
    }

    public static String[] analysisGroupMemberAvatar(String groupAvatar) {
        Map<String, String> idWithAvatar = convertJsonToMapString(groupAvatar);
        if (idWithAvatar == null) return null;
        List<String> ids = new ArrayList<>();
        List<String> urls = new ArrayList<>();
        for (Map.Entry<String, String> entry : idWithAvatar.entrySet()) {
            if (!TextUtils.isEmpty(entry.getValue())) {
                ids.add(entry.getKey());
                urls.add(entry.getValue());
            }
        }
        String[] v = new String[2];
        v[0] = TextUtils.join(",", ids);
        v[1] = TextUtils.join(",", urls);
        return v;
    }
}
